package com.uiotsoft.small.host.sampler;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import com.uiotsoft.util.decrypt.DecryptResponse;
import com.uiotsoft.util.HttpUtil;
import com.uiotsoft.util.encrypt.AesOld;
import com.uiotsoft.util.encrypt.Md5;
import com.uiotsoft.util.encrypt.SignCore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class HostHttpSampler {
    private String appKey;
    private String appSecret;
    private String gateway;
    private static final Logger logger = LoggerFactory.getLogger(HostHttpSampler.class);
    private static volatile HostHttpSampler instance = null;

    private HostHttpSampler(String appKey, String appSecret, String gateway) {
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.gateway = gateway;
    }

    public static HostHttpSampler getInstance(String appKey, String appSecret, String gateway) {
        if (instance == null) {
            synchronized(HostHttpSampler.class) {
                if (instance == null) {
                    instance = new HostHttpSampler(appKey, appSecret, gateway);
                }
            }
        }

        return instance;
    }

    private Map<String, String> getHostHeader(String method, String data) {
        Map<String, String> headerMap = new HashMap();
        Long timestamp = System.currentTimeMillis() / 1000L;
        headerMap.put("appkey", this.appKey);
        headerMap.put("method", method);
        headerMap.put("timestamp", timestamp + "");
        headerMap.put("nonce", "11111");
        headerMap.put("signType", "md5");
        headerMap.put("encryptType", "AES");
        headerMap.put("version", "1.0");
        headerMap.put("isEncrypt", "true");
        headerMap.put("data", data);
        String sign = Md5.sign(SignCore.createLinkString(headerMap), this.appSecret, "");
        headerMap.remove("data");
        headerMap.put("sign", sign);
        headerMap.put("Content-Type", "application/json");
        return headerMap;
    }

    public String hostRequestPost(String method, String unEncryptData) {
        logger.info(method + "接口的发送数据为：" + unEncryptData);
        String encryptData = AesOld.encryptAes(unEncryptData, this.appSecret);
        logger.debug("加密后的数据为：" + encryptData);
        Map<String, String> headerMap = this.getHostHeader(method, encryptData);
        logger.debug("请求头为：" + headerMap.toString());
        String result = HttpUtil.requestPost(this.gateway, headerMap, encryptData);
        logger.debug("返回结果未解密为：" + result);
        String resultDecrypt = DecryptResponse.parseResponse(result, this.appSecret);
        logger.info(method + "接口的返回结果为：" + resultDecrypt);
        return resultDecrypt;
    }

    public String hostRequestGet(String method, String unEncryptData) {
        logger.info(method + "接口的发送数据为：" + unEncryptData);
        String encryptData = AesOld.encryptAes(unEncryptData, this.appSecret);
        logger.debug("加密后的数据为：" + encryptData);
        Map<String, String> headerMap = this.getHostHeader(method, encryptData);
        String gatewayNew;
        if (encryptData != null) {
            gatewayNew = this.gateway + "?data=" + encryptData;
        } else {
            gatewayNew = this.gateway;
        }

        String result = HttpUtil.requestGet(gatewayNew, headerMap);
        logger.debug("返回结果未解密为：" + result);
        String resultDecrypt = DecryptResponse.parseResponse(result, this.appSecret);
        logger.info(method + "接口的返回结果为：" + resultDecrypt);
        return resultDecrypt;
    }
}

